package com.study.algorithm.utils;

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

public class LamdaUtil {

    /**
     * 通过lamda表达式判断是否为重复字段
     *
     * @param keyExtractor 该键值是作为是否为重复字段的关键（map中的键值）
     *                     他的来源为你传入得方法函数的返回值
     * @return boolean
     */
    public static <T> Predicate<T> DistinctByKey(Function<? super T, ?> keyExtractor) {
        ConcurrentHashMap<Object, Boolean> map = new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
}
